package com.ctg.behavior.calc.impala.builder.funnel;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.impala.report.funnel.CtCalcFunnelStepUserIdsReport;
import com.ctg.behavior.common.consts.CtCalcConsts;
import com.ctg.behavior.common.funnel.CtFunnelCalcBody;
import com.ctg.behavior.common.funnel.CtFunnelDetailBody;
import com.ctg.behavior.common.funnel.CtFunnelSliceBody;
import com.ctg.behavior.common.funnel.CtFunnelStep;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CtFunnelStepUserIdsReprotBuilder {

	public CtCalcFunnelStepUserIdsReport doBuilder(CtFunnelSliceBody funnelExtCalcBody) {

		CtCalcFunnelStepUserIdsReport report = new CtCalcFunnelStepUserIdsReport();

		// 扩展字段
		report.setSliceStep(funnelExtCalcBody.getSliceStep());
		report.setSliceByValues(funnelExtCalcBody.getSliceByValues());

		//
		CtFunnelCalcBody funnelCalcBody = this.buildFunnelCalcBody(funnelExtCalcBody);
		CtFunnelDetailBody funnelDetailBody = funnelCalcBody.getFunnelDetail();
		List<CtFunnelStep> funnelSteps = funnelDetailBody.getSteps();

		List<String> eventIds = new ArrayList<>();
		List<String> eventCNames = new ArrayList<>();

		Integer stepNo = Integer.valueOf(report.getSliceStep());

		if (stepNo >= funnelSteps.size()) {
			stepNo = funnelSteps.size() - 1;
			log.info("stepNo={},funnelSteps={}, funnelSteps={}", stepNo, funnelSteps.size(), JSON.toJSONString(funnelSteps));
		}
		CtFunnelStep step = funnelSteps.get(stepNo);

		// 封装eventId
		eventIds.add(step.getEventId());
		if (StringUtils.isEmpty(step.getEventCName())) {
			eventCNames.add(step.getEventId());
		} else {
			eventCNames.add(step.getEventCName());
		}

		report.setFilter(step.getFilter());
		report.setFromDate(funnelCalcBody.getFromDate());
		report.setToDate(funnelCalcBody.getToDate());
		report.setByFields(funnelCalcBody.getByFields());
		report.setEventId(step.getEventId());
		report.setGlobalFilter(funnelCalcBody.getFilter());
		report.setEventIds(eventIds);
		report.setEventCNames(eventCNames);

		report.setAppId(funnelCalcBody.getAppId());
		report.setStepNo(stepNo);

		return report;
	}

	private CtFunnelCalcBody buildFunnelCalcBody(CtFunnelSliceBody body) {

		CtFunnelCalcBody funnelCalcBody = new CtFunnelCalcBody();

		funnelCalcBody.setAppId(body.getAppId());
		funnelCalcBody.setRequestId(body.getRequestId());
		funnelCalcBody.setBookmarkId(body.getBookmarkId());

		funnelCalcBody.setFromDate(body.getFromDate());
		funnelCalcBody.setToDate(body.getToDate());
		funnelCalcBody.setUnit(CtCalcConsts.UNIT_TYPE_DAY);
		funnelCalcBody.setFilter(body.getFilter());
		funnelCalcBody.setSubTaskType(body.getSubTaskType());
		funnelCalcBody.setSubjectId(body.getSubjectId());

		funnelCalcBody.setFunnelId(Long.valueOf(body.getFunnelId()));
		funnelCalcBody.setFunnelDetail(body.getFunnelDetail());
		funnelCalcBody.setByFieldSteps(body.getByFieldSteps());
		funnelCalcBody.setByFields(body.getByFields());

		return funnelCalcBody;
	}

}
